<?php
include 'code/DB.php';
/**
 * Description of Tasks
 *
 * @author jay
 */
class Tasks {
    private $db;
      
    public function __construct() {
        $this->db = DB::getInstance();
    }

    function saveTask($projectid,$userid, $type, $task, $status) {
        $status;
        $db = $this->db;
        
        try {
            $sql = "INSERT INTO tasks (projectid,createdby,type,description,status,assignedto,createdon,lastmodifiedon) 
                VALUES (:projectid,:createdby,:type,:description,:status,:assignedto,NOW(),NOW())";
            $db->query($sql);
            $db->bind(":projectid", $projectid);
            $db->bind(":createdby", $userid);
            $db->bind(":type", $type);
            $db->bind(":description", $task);
            $db->bind(":status", $status);
            $db->bind(":assignedto", $userid);
            $db->execute();  
            
            $status = true;
        } catch (Exception $e) {
            $status = false;
            echo 'Cached exception inserting task: ' . $e . getMessage();
        }
        //$db->closeDB();
        return $status;
    }
        
    function updateTask($userid, $type, $task, $status, $taskid) {
        $db = $this->db;
        
        try {
            $sql = "UPDATE tasks SET description = :description, type = :type, 
                status = :status, createdby = :userid, lastmodifiedon = NOW() WHERE id = :id";
            
            $db->query($sql);
            $db->bind(":description", $task);
            $db->bind(":type", $type);
            $db->bind(":status", $status);
            $db->bind(":userid", $userid);
            $db->bind(":id", $taskid);
            $db->execute();            
        } catch (Exception $e) {
            echo 'Exception updating task: ' . $e . getMessage();
        }
        //$db->closeDB();
    }
    
    function saveTaskUpdates($userid, $projectid, $taskid, $updatetext) {
        $result = "";
        $db = $this->db;

        try {
            $sql = "INSERT INTO taskcomments (taskid, userid, comment, postedon)
                VALUES (:taskid, :userid, :comment, NOW())";
            $db->query($sql);
            $db->bind(":taskid", $taskid);
            $db->bind(":userid", $userid);
            $db->bind(":comment", $updatetext);
            $db->execute();        
                    
            // Get task update notes and return
            $result = $this->getTaskUpdates($taskid);
        } catch (Exception $e) {
            echo 'Exception inserting task update notes: ' . $e . getMessage();
        }

        //$db->closeDB();
        return $result;
    }
        
    function getTaskUpdates($taskid) {         
        $db = $this->db;
        
        $sql = "SELECT t.id, t.taskid,t.comment,t.postedon, concat(u.ufname, \" \", u.ulname) as postedby 
                    FROM taskcomments t LEFT JOIN users u
                    ON t.userid = u.id WHERE t.taskid = :taskid AND t.isdeleted = '0'
                    ORDER BY id desc";
        $db->query($sql);
        $db->bind(":taskid", $taskid);
        $rows = $db->resultset();
        
        $str = "<table class=\"table table-bordered table-condensed\" width=\"100%\"><tbody>";
        foreach($rows as $row) {
            $str = $str . "<tr>";
            $str = $str . "<td width=\"160px\"><div>" . date('F j H:i', strtotime($row['postedon'])) . "</div><div class=\"muted\">" . $row['postedby'] . "</div></td>";
            $str = $str . "<td>" . $row['comment'] . "</td>";
            $str = $str . "<tr>";                                               
        }
        $str = $str . "</tbody></table>";
        
        //$db->closeDB();
        return $str;
    }
    
    function getTasks($userid, $status, $projectid) {        
        $db = $this->db;
        
        $sql = "SELECT * FROM tasks WHERE isdeleted = 0 ";
        if($userid > 0) {
            $sql = $sql . " AND assignedto = '" . $userid . "'";
        }
        if($status != "All") {
            $sql = $sql . " AND status LIKE '" . $status . "'";
        } else {
            $sql = $sql . " AND status <> 'Done'";
        }
        if($projectid > 0) {
            $sql = $sql . " AND projectid = '" . $projectid . "'";
        }
        $sql = $sql . " ORDER BY priority asc LIMIT 1000";
        
        $db->query($sql);
        $rows = $db->resultset();
        
        //$db->closeDB();
        return $rows;   
    }
    
    function getActiveProjects($userid) {
        $db = $this->db;
        
        $sql = "SELECT * FROM projects WHERE isdeleted = 0 AND createdby = " . $userid;
        
        $sql = $sql . " ORDER BY id asc LIMIT 50";
        
        $db->query($sql);
        $rows = $db->resultset();
        
        //$db->closeDB();
        return $rows; 
    }
	
	function getActiveUsers($userid) {
        $db = $this->db;
        
        $sql = "SELECT * FROM users WHERE isdeleted = 0";
        
        $sql = $sql . " ORDER BY id asc LIMIT 50";
        
        $db->query($sql);
        $rows = $db->resultset();
        
        //$db->closeDB();
        return $rows; 
    }
    
    function getAllProjects($userid, $projectid) {
        $db = $this->db;
        
        $sql = "SELECT * FROM projects WHERE isdeleted = 0 AND createdby = :userid ";
        if($projectid > 0) {
            $sql = $sql . " AND id = " . $projectid;
        }
        
        $sql = $sql . " ORDER BY id asc LIMIT 50";
        
        $db->query($sql);
        $db->bind(":userid", $userid);
        $db->execute();
        $rows = $db->resultset();
                
        //$db->closeDB();
        return $rows; 
    }
    
    function saveProject($userid, $projectid, $projectname, $description) {
        $result = false;
        $db = $this->db;

        try {
            $sql = "INSERT INTO projects (project, description, visibility, createdby,createdon)
                VALUES (:project, :description, 'public', :userid, NOW())";
            $db->query($sql);
            $db->bind(":project", $projectname);
            $db->bind(":description", $description);
            $db->bind(":userid", $userid);
            $db->execute();
            $result = true;
        } catch (Exception $e) {
            echo 'Exception creating project: ' . $e . getMessage();
            $result = false;
        }

        //$db->closeDB();
        return $result;
    }
    
    function getDoneTaskCount($projectid, $userid) {
        $db = $this->db;
        
        $sql = "SELECT id as count FROM tasks WHERE isdeleted = 0 AND createdby = :userid AND projectid = :projectid AND status LIKE 'Done'";
        $db->query($sql);
        
        $db->bind(":userid", $userid);
        $db->bind(":projectid", $projectid);
        
        $db->execute();
        $rows = $db->rowCount();
        
        //$db->closeDB();
        return $rows; 
    }
    
    function updateTaskOrder($userid, $projectid, $priorities) {
        $db = $this->db;
        
        try {
            $ids=explode(",",$priorities);
            
            
            for ($i = 0; $i < count($ids); $i++) {
                $sql = "UPDATE tasks SET priority = '" . $i . "' WHERE id = '" . $ids[$i] . "'";
                $db->query($sql, array($i, $ids[$i]));
                $db->execute();
            }
                    
        } catch (Exception $e) {
            echo 'Exception updating task: ' . $e . getMessage();
        }
        //$db->closeDB();
    }
}

?>
